home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-02 / nktools.zip / STRUTIL.PAS < prev    next >
Pascal/Delphi Source File  |  1990-05-14  |  26KB  |  622 lines

  1. UNIT StrUtil;
  2. (*====================================================================*\
  3. || MODULE NAME:  StrUtil                                              ||
  4. || DEPENDENCIES: System                                               ||
  5. || LAST MOD ON:  9005.14                                              ||
  6. || PROGRAMMERS:  Andrea Spilholtz, Mike Temkin, SteveAlter,           ||
  7. ||               Naoto Kimura                                         ||
  8. ||                                                                    ||
  9. ||     This is a library of string handling routines.  Many have been ||
  10. || rewritten in assembler for the sake of speed.                      ||
  11. ||                                                                    ||
  12. || Modification history                                               ||
  13. ||                                                                    ||
  14. || 8907.10    Naoto Kimura                                            ||
  15. ||            * Last update before the code was prepared for spring   ||
  16. ||              semester.                                             ||
  17. || 8912.10    Naoto Kimura                                            ||
  18. ||            * Added LoCase, UpperCaseStr, and LowerCaseStr          ||
  19. ||              functions.                                            ||
  20. || 9001.17    Naoto Kimura                                            ||
  21. ||            * Started to modify some functions for rewriting in     ||
  22. ||              assembly.                                             ||
  23. || 9001.19    Naoto Kimura                                            ||
  24. ||            * Minor modifications for efficiency.                   ||
  25. ||            * Renamed some functions: UpperCaseStr --> UpperStr     ||
  26. ||              and LowerCaseStr --> LowerStr.                        ||
  27. ||            * Changed UpperCase, LowerCase, Alphabet and AlphaNum   ||
  28. ||              into regular variables instead of typed constants.    ||
  29. ||              (Just in case the UpCase function gets redefined)     ||
  30. ||            * Added two new functions, LoCase2 and UpCase2 to       ||
  31. ||              perform lowercasing and uppercasing as defined by the ||
  32. ||              user (by changing the variables LowerTbl and UpperTbl ||
  33. ||              look-up tables)                                       ||
  34. || 9001.20    Naoto Kimura                                            ||
  35. ||            * The following routines have been rewritten in         ||
  36. ||              assembly to speed them up and to reduce memory usage: ||
  37. ||                 LoCase, LoCase2, UpCase2                           ||
  38. ||                 UpperStr, LowerStr                                 ||
  39. ||                 RightPos                                           ||
  40. ||                 RightJustify, LeftJustify, Center, Reverse         ||
  41. || 9001.20    Naoto Kimura                                            ||
  42. ||            * The following was rewritten in assembly:              ||
  43. ||                 Copies                                             ||
  44. || 9002.26    Naoto Kimura                                            ||
  45. ||            * Added function LeftPos which does a similar task as   ||
  46. ||              the RightPos function.                                ||
  47. ||            * Added function Strip to perform stripping of unwanted ||
  48. ||              characters.  Eventually, this too shall be rewritten  ||
  49. ||              in assembler.                                         ||
  50. || 9005.06    Naoto Kimura                                            ||
  51. ||            * Rewrote RPos in assembler and split up the assembler  ||
  52. ||              modules to aid the unused code removal.               ||
  53. || 9005.14    Naoto Kimura                                            ||
  54. ||            * Rewrote Strip in assembler.                           ||
  55. \*====================================================================*)
  56. {$R-}    {Range checking off}
  57. {$S+}    {Stack checking on}
  58. {$D-}    {Debug info off}
  59. {$I-}    {I/O checking off}
  60. {$N-}    {No numeric coprocessor}
  61.  
  62. INTERFACE
  63.  
  64. TYPE
  65.     CharLookTbl    = ARRAY [Char] OF Char;
  66.     CharSet    = SET OF Char;
  67.  
  68. CONST
  69.     WhiteSpace    : CharSet    = [' ',#9,#10,#13];
  70.     Numeric    : CharSet    = ['0'..'9'];
  71.  
  72. VAR
  73.     UpperCase    : CharSet;
  74.     LowerCase    : CharSet;
  75.     Alphabet    : CharSet;
  76.     AlphaNum    : CharSet;
  77. {$IFDEF DEBUG}
  78.     StdLower    : CharLookTbl;
  79. {$ENDIF}
  80.     LowerTbl,
  81.     UpperTbl    : CharLookTbl;
  82.  
  83. (*--------------------------------------------------------------------*\
  84. | NAME:  StrInt                                                        |
  85. |                                                                      |
  86. |     This function returns string representation of an integer value. |
  87. | This function really returns the value of the Str procedure, but     |
  88. | this way we can use the value w/o having to explicitly call Str with |
  89. | a temporary string.  This really only comes in handy if you want to  |
  90. | the conversion and then use the string value to do concatenation or  |
  91. | pass the string value into a function.                               |
  92. \*--------------------------------------------------------------------*)
  93. FUNCTION StrInt (
  94.         I: Integer
  95.     ): String;
  96.  
  97. (*--------------------------------------------------------------------*\
  98. | NAME:  StrReal                                                       |
  99. |                                                                      |
  100. |     This function returns string representation of a real value.     |
  101. | This function really returns the value of the Str procedure, but     |
  102. | this way we can use the value w/o having to explicitly call Str with |
  103. | a temporary string.  This really only comes in handy if you want to  |
  104. | the conversion and then use the string value to do concatenation or  |
  105. | pass the string value into a function.                               |
  106. \*--------------------------------------------------------------------*)
  107. FUNCTION StrReal (
  108.         R: Real
  109.     ): String;
  110.  
  111. (*--------------------------------------------------------------------*\
  112. | NAME: LoCase                                                         |
  113. |                                                                      |
  114. |     This function performs the opposite function as the UpCase       |
  115. | function; it takes an upper case character and transforms it into    |
  116. | its lower case form.                                                 |
  117. \*--------------------------------------------------------------------*)
  118. FUNCTION LoCase (
  119.         C: Char
  120.     ): Char;
  121.  
  122. (*--------------------------------------------------------------------*\
  123. | NAME: LoCase2                                                        |
  124. |                                                                      |
  125. |     This function performs a similar function as the LoCase          |
  126. | function; it takes an upper case character and transforms it into    |
  127. | its lower case form.  The difference is that the the lowercasing can |
  128. | be altered by the user.                                              |
  129. \*--------------------------------------------------------------------*)
  130. FUNCTION LoCase2 (
  131.         C: Char
  132.     ): Char;
  133.  
  134. (*--------------------------------------------------------------------*\
  135. | NAME: UpCase2                                                        |
  136. |                                                                      |
  137. |     This function performs a similar function as the UpCase          |
  138. | function; it takes an lower case character and transforms it into    |
  139. | its upper case form.  The difference is that the the uppercasing can |
  140. | be altered by the user.                                              |
  141. \*--------------------------------------------------------------------*)
  142. FUNCTION UpCase2 (
  143.         C: Char
  144.     ): Char;
  145.  
  146. (*--------------------------------------------------------------------*\
  147. | NAME:  UpperStr                                                      |
  148. |                                                                      |
  149. |     This function returns the passed string with all the lower case  |
  150. | characters transformed into upper case characters.                   |
  151. \*--------------------------------------------------------------------*)
  152. FUNCTION UpperStr (
  153.         S    :String
  154.     ): String;
  155.  
  156. (*--------------------------------------------------------------------*\
  157. | NAME:  LowerStr                                                      |
  158. |                                                                      |
  159. |     This function returns the passed string with all the upper case  |
  160. | characters transformed into lower case characters.                   |
  161. \*--------------------------------------------------------------------*)
  162. FUNCTION LowerStr (
  163.         S    :String
  164.     ): String;
  165.  
  166. (*--------------------------------------------------------------------*\
  167. | NAME:  RightPos                                                      |
  168. |                                                                      |
  169. |     This function returns the last matching position of character    |
  170. | "C" in "S".                                                          |
  171. \*--------------------------------------------------------------------*)
  172. FUNCTION RightPos (
  173.         S    : String;
  174.         C    : Char
  175.     ): Integer;
  176.  
  177. (*--------------------------------------------------------------------*\
  178. | NAME:  LeftPos                                                       |
  179. |                                                                      |
  180. |     This function returns the first matching position of character   |
  181. | "C" in "S".                                                          |
  182. \*--------------------------------------------------------------------*)
  183. FUNCTION LeftPos (
  184.         S    : String;
  185.         C    : Char
  186.     ): Integer;
  187.  
  188. (*--------------------------------------------------------------------*\
  189. | NAME:  RPos                                                          |
  190. |                                                                      |
  191. |     This function returns the last matching position of "Needle" in  |
  192. | "HayStack."                                                          |
  193. \*--------------------------------------------------------------------*)
  194. FUNCTION RPos(
  195.         Needle,
  196.         HayStack    : string
  197.     ) : byte;
  198.  
  199. (*--------------------------------------------------------------------*\
  200. | NAME:  CharSetPos                                                    |
  201. |                                                                      |
  202. |     This routine returns the first position of a member of a set     |
  203. | "Srch" within the string "HayStack."                                 |
  204. \*--------------------------------------------------------------------*)
  205. FUNCTION CharSetPos(
  206.         Srch    : CharSet;
  207.         HayStack    : string
  208.     ) : byte;
  209.  
  210. (*--------------------------------------------------------------------*\
  211. | NAME:  RCharSetPos                                                   |
  212. |                                                                      |
  213. |     This routine returns the last position of a member of a set      |
  214. | "Srch" within the string "HayStack."                                 |
  215. \*--------------------------------------------------------------------*)
  216. FUNCTION RCharSetPos(
  217.         Srch    : CharSet;
  218.         HayStack    : string
  219.     ) : byte;
  220.  
  221. (*--------------------------------------------------------------------*\
  222. | NAME: CharSetStrip                                                   |
  223. |                                                                      |
  224. |     This function strips off the specified characters from Original. |
  225. | Leading characters to strip off are specified in LeadSet and         |
  226. | trailing characters to strip off are specifed in TrailSet.           |
  227. \*--------------------------------------------------------------------*)
  228. FUNCTION CharSetStrip (
  229.         Original    : string;
  230.         LeadSet,
  231.         TrailSet    : CharSet
  232.     ) : string;
  233.  
  234. (*--------------------------------------------------------------------*\
  235. | NAME:  Copies                                                        |
  236. |                                                                      |
  237. |     This function returns as many copies of a string concatenated    |
  238. | together as requested.                                               |
  239. \*--------------------------------------------------------------------*)
  240. FUNCTION Copies (
  241.         Original    : String;
  242.         Num        : Byte
  243.     ) : String;
  244.  
  245. (*--------------------------------------------------------------------*\
  246. | NAME:  RightJustify                                                  |
  247. |                                                                      |
  248. |     This function returns a string that has the string "Original"    |
  249. | right justified in a field of length "width" of the character        |
  250. | "filler".  If the string is longer than the field, the string will   |
  251. | be truncated at the field width.                                     |
  252. \*--------------------------------------------------------------------*)
  253. FUNCTION RightJustify (
  254.         Original    : string;
  255.         width    : byte;
  256.         filler    : char
  257.     ) : string;
  258.  
  259. (*--------------------------------------------------------------------*\
  260. | NAME:  LeftJustify                                                   |
  261. |                                                                      |
  262. |     This function returns a string that has the string "Original"    |
  263. | left justified in a field of length "width" of the character         |
  264. | "filler".  If the string is longer than the field, the string will   |
  265. | be truncated at the field width.                                     |
  266. \*--------------------------------------------------------------------*)
  267. FUNCTION LeftJustify (
  268.         Original    : string;
  269.         width    : byte;
  270.         filler    : char
  271.     ) : string;
  272.  
  273. (*--------------------------------------------------------------------*\
  274. | NAME:  Center                                                        |
  275. |                                                                      |
  276. |     This function returns a string that has the string "Original"    |
  277. | centered in a field of length "width" of the character "filler".  If |
  278. | the string is longer than the field, the string will be truncated at |
  279. | the field width.                                                     |
  280. \*--------------------------------------------------------------------*)
  281. FUNCTION Center (
  282.         Original    : string;
  283.         width    : byte;
  284.         filler    : char
  285.     ) : string;
  286.  
  287. (*--------------------------------------------------------------------*\
  288. | NAME: Strip                                                          |
  289. |                                                                      |
  290. |     This function strips off unwanted characters from either the     |
  291. | left, right or both ends of a string.  
  292. \*--------------------------------------------------------------------*)
  293. function Strip (
  294.         Original    : String;
  295.         Unwanted    : String;
  296.         Location    : Char
  297.     ) : String;
  298.  
  299. (*--------------------------------------------------------------------*\
  300. | NAME:  SkipStr                                                       |
  301. |                                                                      |
  302. |     This routine is used to grab a copy of the string, past the      |
  303. | location of the given pattern.                                       |
  304. \*--------------------------------------------------------------------*)
  305. FUNCTION SkipStr (
  306.         Original,
  307.         pattern    : string
  308.     ) : string;
  309.  
  310. (*--------------------------------------------------------------------*\
  311. | NAME:  Reverse                                                       |
  312. |                                                                      |
  313. |     This function returns a copy of a string that is reversed.       |
  314. \*--------------------------------------------------------------------*)
  315. FUNCTION Reverse (
  316.         Original    : string
  317.     ) : string;
  318.  
  319. (*--------------------------------------------------------------------*\
  320. | NAME:  FindPos                                                       |
  321. |                                                                      |
  322. |     This function returns the position of the character "C" within   |
  323. | string "S," ignoring any occurances before the "P"th position with   |
  324. | "S."                                                                 |
  325. \*--------------------------------------------------------------------*)
  326. FUNCTION FindPos (
  327.         S    : String;
  328.         C    : Char;
  329.         P    : Integer
  330.     ): Integer;
  331.  
  332. IMPLEMENTATION
  333.  
  334. VAR
  335.     WorkBuffer    : String;
  336. {$IFNDEF DEBUG}
  337.     StdLower    : CharLookTbl;
  338. {$ENDIF}
  339.  
  340. (*--------------------------------------------------------------------*\
  341. | NAME:  StrInt                                                        |
  342. \*--------------------------------------------------------------------*)
  343. FUNCTION StrInt (
  344.         I: Integer
  345.     ): String;
  346.     BEGIN
  347.     Str(I,WorkBuffer);  StrInt := WorkBuffer;
  348.     END;  (* StrInt *)
  349.  
  350. (*--------------------------------------------------------------------*\
  351. | NAME:  StrReal                                                       |
  352. \*--------------------------------------------------------------------*)
  353. FUNCTION StrReal (
  354.         R: Real
  355.     ): String;
  356.     BEGIN
  357.     Str(R:1:5,WorkBuffer);  StrReal := WorkBuffer;
  358.     END;  (* StrReal *)
  359.  
  360. {$L Cases.OBJ}
  361.  
  362. (*--------------------------------------------------------------------*\
  363. | NAME: LoCase                                                         |
  364. \*--------------------------------------------------------------------*)
  365. FUNCTION LoCase (C: Char): Char;
  366.     External;
  367.  
  368. (*--------------------------------------------------------------------*\
  369. | NAME: LoCase2                                                        |
  370. \*--------------------------------------------------------------------*)
  371. FUNCTION LoCase2 (C: Char): Char;
  372.     External;
  373.  
  374. (*--------------------------------------------------------------------*\
  375. | NAME: UpCase2                                                        |
  376. \*--------------------------------------------------------------------*)
  377. FUNCTION UpCase2 (C: Char): Char;
  378.     External;
  379.  
  380. (*--------------------------------------------------------------------*\
  381. | NAME:  UpperStr                                                      |
  382. \*--------------------------------------------------------------------*)
  383. FUNCTION UpperStr ( S :String ): String;
  384.     External;
  385.  
  386. (*--------------------------------------------------------------------*\
  387. | NAME:  LowerStr                                                      |
  388. \*--------------------------------------------------------------------*)
  389. FUNCTION LowerStr ( S :String ): String;
  390.     External;
  391.  
  392. {$L StrPos.OBJ}
  393.  
  394. (*--------------------------------------------------------------------*\
  395. | NAME:  RPos                                                          |
  396. \*--------------------------------------------------------------------*)
  397. FUNCTION RPos(
  398.         Needle,
  399.         HayStack    : string
  400.     ) : byte;
  401.     External;
  402.  
  403. (*--------------------------------------------------------------------*\
  404. | NAME:  RightPos                                                      |
  405. \*--------------------------------------------------------------------*)
  406. FUNCTION RightPos ( S:String;  C:Char ) : Integer;
  407.     External;
  408.  
  409. (*--------------------------------------------------------------------*\
  410. | NAME:  LeftPos                                                       |
  411. \*--------------------------------------------------------------------*)
  412. FUNCTION LeftPos ( S:String;  C:Char ) : Integer;
  413.     External;
  414.  
  415. (*--------------------------------------------------------------------*\
  416. | NAME:  CharSetPos                                                    |
  417. \*--------------------------------------------------------------------*)
  418. FUNCTION CharSetPos(
  419.         Srch    : CharSet;
  420.         HayStack    : string
  421.     ) : byte;
  422.     VAR
  423.     i    : byte;
  424.     BEGIN
  425.     IF (HayStack = '') OR (Srch = []) THEN
  426.         CharSetPos := 0
  427.     ELSE BEGIN
  428.         FOR i := 1 TO length(HayStack) DO
  429.         IF HayStack[i] IN Srch THEN BEGIN
  430.             CharSetPos := i;
  431.             exit
  432.           END;
  433.         CharSetPos := 0
  434.       END
  435.     END;    (* CharSetPos *)
  436.  
  437. (*--------------------------------------------------------------------*\
  438. | NAME:  RCharSetPos                                                   |
  439. \*--------------------------------------------------------------------*)
  440. FUNCTION RCharSetPos(
  441.         Srch    : CharSet;
  442.         HayStack    : string
  443.     ) : byte;
  444.     VAR
  445.     i    : byte;
  446.     BEGIN
  447.     IF (HayStack = '') OR (Srch = []) THEN
  448.         RCharSetPos := 0
  449.     ELSE BEGIN
  450.         FOR i := length(HayStack) DOWNTO 1 DO
  451.         IF HayStack[i] IN Srch THEN BEGIN
  452.             RCharSetPos := i;
  453.             exit
  454.           END;
  455.         RCharSetPos := 0
  456.       END
  457.     END;    (* RCharSetPos *)
  458.  
  459. (*--------------------------------------------------------------------*\
  460. | NAME: CharSetStrip                                                   |
  461. \*--------------------------------------------------------------------*)
  462. FUNCTION CharSetStrip(
  463.         Original    : string;
  464.         LeadSet,
  465.         TrailSet    : CharSet
  466.     ) : string;
  467.     VAR
  468.     Left,
  469.     Right    : byte;
  470.     stop    : boolean;
  471.     BEGIN
  472.     Left := 1;
  473.     Right := length(Original);
  474.     IF Left>Right THEN
  475.         stop := FALSE
  476.     ELSE
  477.         stop := NOT (Original[Left] IN LeadSet)
  478.             AND NOT (Original[Right] IN TrailSet);
  479.     WHILE NOT (stop OR (Right<Left)) DO BEGIN
  480.         stop := TRUE;
  481.         IF Original[Left] IN LeadSet THEN BEGIN
  482.         inc(Left);
  483.         stop := FALSE
  484.           END;
  485.         IF Original[Right] IN TrailSet THEN BEGIN
  486.         dec(Right);
  487.         stop := FALSE
  488.           END
  489.       END;
  490.     IF stop THEN
  491.         CharSetStrip := copy(Original,Left,Right-Left+1)
  492.     ELSE
  493.         CharSetStrip := ''
  494.     END;    (* CharSetStrip *)
  495.  
  496. {$L StrFmt.OBJ}
  497.  
  498. (*--------------------------------------------------------------------*\
  499. | NAME:  Copies                                                        |
  500. \*--------------------------------------------------------------------*)
  501. FUNCTION Copies (
  502.         Original    : String;
  503.         Num        : Byte
  504.     ) : String;
  505.     External;
  506.  
  507. (*--------------------------------------------------------------------*\
  508. | NAME:  RightJustify                                                  |
  509. \*--------------------------------------------------------------------*)
  510. FUNCTION RightJustify(
  511.         Original    : string;
  512.         width    : byte;
  513.         filler    : char
  514.     ) : string;
  515.     External;
  516.  
  517. (*--------------------------------------------------------------------*\
  518. | NAME:  LeftJustify                                                   |
  519. \*--------------------------------------------------------------------*)
  520. FUNCTION LeftJustify(
  521.         Original    : string;
  522.         width    : byte;
  523.         filler    : char
  524.     ) : string;
  525.     External;
  526.  
  527. (*--------------------------------------------------------------------*\
  528. | NAME:  Center                                                        |
  529. \*--------------------------------------------------------------------*)
  530. FUNCTION Center(
  531.         Original    : string;
  532.         width    : byte;
  533.         filler    : char
  534.     ) : string;
  535.     External;
  536.  
  537. (*--------------------------------------------------------------------*\
  538. | NAME: Strip                                                          |
  539. \*--------------------------------------------------------------------*)
  540. function Strip (
  541.         Original    : String;
  542.         Unwanted    : String;
  543.         Location    : Char
  544.     ) : String;
  545.     External;
  546.  
  547. (*--------------------------------------------------------------------*\
  548. | NAME:  SkipStr                                                       |
  549. \*--------------------------------------------------------------------*)
  550. FUNCTION SkipStr(
  551.         original,
  552.         pattern    : string
  553.     ) : string;
  554.     BEGIN
  555.     SkipStr := copy(original,
  556.             pos(pattern,original)+length(pattern),
  557.             length(original))
  558.     END;    (* SkipStr *)
  559.  
  560. (*--------------------------------------------------------------------*\
  561. | NAME:  Reverse                                                       |
  562. \*--------------------------------------------------------------------*)
  563. FUNCTION Reverse( Original : String ) : String;
  564.     External;
  565.  
  566. (*--------------------------------------------------------------------*\
  567. | NAME:  FindPos                                                       |
  568. \*--------------------------------------------------------------------*)
  569. FUNCTION FindPos (
  570.         S    : String;
  571.         C    : Char;
  572.         P    : Integer
  573.     ): Integer;
  574.     VAR
  575.     T    : Integer;
  576.     BEGIN
  577.     IF (P < 1) OR (P > Length(S)) THEN
  578.         FindPos := 0
  579.     ELSE BEGIN
  580.         T := Pos(C,Copy(S,P,Length(S)));
  581.         IF T <> 0 THEN
  582.         T := T - 1 + P;
  583.         FindPos := T
  584.       END
  585.     END;  (* FindPos *)
  586.  
  587. PROCEDURE Init;
  588.     VAR
  589.     C    : Char;
  590.     BEGIN
  591.     LowerCase := [];
  592.     UpperCase := [];
  593.  
  594. {$IFDEF DEBUG}
  595.     FillChar(StdLower,SizeOf(StdLower),128);
  596.     FillChar(LowerTbl,SizeOf(LowerTbl),128);
  597.     FillChar(UpperTbl,SizeOf(UpperTbl),128);
  598. {$ENDIF}
  599.  
  600.     FOR C := chr(0) TO chr(255) DO BEGIN
  601.         UpperTbl[C] := C;
  602.         StdLower[C] := C;
  603.         LowerTbl[C] := C
  604.       END;
  605.  
  606.     FOR C := chr(0) TO chr(255) DO
  607.         IF UpCase(C) <> C THEN BEGIN
  608.         StdLower[UpCase(C)] := C;
  609.         LowerTbl[UpCase(C)] := C;
  610.         UpperTbl[C] := UpCase(C);
  611.         UpperCase := UpperCase + [UpCase(C)];
  612.         LowerCase := LowerCase + [C]
  613.           END;
  614.  
  615.     Alphabet := LowerCase + UpperCase;
  616.     AlphaNum := Alphabet + Numeric
  617.     END;
  618.  
  619. BEGIN
  620.     Init;
  621. END.
  622.